package com.deercrowing.takeaway.controller.admin;

import com.deercrowing.takeaway.common.exception.BusinessException;
import com.deercrowing.takeaway.common.model.Result;
import com.deercrowing.takeaway.entity.DishesEntity;
import com.deercrowing.takeaway.vo.DishesPageVO;
import com.deercrowing.takeaway.vo.DishesVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ObjectUtils;
import jakarta.validation.constraints.NotNull;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import com.deercrowing.takeaway.service.DishesService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 菜品表(Dishes)表控制层
 *
 * @author guoyongxiang
 * @since 2024-07-11 10:51:15
 */
@Slf4j
@Validated
@RestController
@RequestMapping("admin/dishes")
public class DishesController {
    /**
     * 服务对象
     */
    @Autowired
    private DishesService dishesService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 分页查询所有数据
     *
     * @param dishesPageVO 分页对象
     * @return 所有数据
     */
    @GetMapping("/list")
    public ResponseEntity selectAll(DishesPageVO dishesPageVO) {
        return ResponseEntity.ok(Result.OK(this.dishesService.searchByCondition(dishesPageVO)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    public ResponseEntity selectOne(@PathVariable @NotNull(message = "主键不能为空") Long id) {
        if (ObjectUtils.isEmpty(this.dishesService.getById(id)))
        {
            throw new NullPointerException();
        }
        return ResponseEntity.ok(Result.OK(this.dishesService.searchById(id)));
    }

    /**
     * 新增数据
     *
     * @param dishesVO 实体对象
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity insert(@RequestBody DishesVO dishesVO) {
        if (ObjectUtils.isEmpty(dishesVO)){
            throw new NullPointerException();
        }

        this.dishesService.insertInfoToDB(dishesVO);

        //清理缓存数据
        String key = "dish_" + dishesVO.getCategoryId();
        cleanCache(key);

        return ResponseEntity.ok(Result.OK());
    }

    /**
     * 修改数据
     *
     * @param id 主键, dishesVO 实体对象
     * @return 修改结果
     */
    @PutMapping("/{id}")
    public ResponseEntity update(@PathVariable @NotNull(message = "主键不能为空") Long id, @RequestBody DishesVO dishesVO) {
        this.dishesService.updateInfoToDB(id,dishesVO);

        //清理缓存数据
        String key = "dish_" + dishesVO.getCategoryId();
        cleanCache(key);

        return ResponseEntity.ok(Result.OK());
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public ResponseEntity delete(@RequestParam("ids") List<Integer> idList) {
        if (idList.size() < 0){
            throw new BusinessException("请勾选要删除的内容");
        }

        //将所有的菜品缓存数据清理掉，所有以dish_开头的key
        cleanCache("dish_*");
        this.dishesService.deleteDishWithFlavor(idList);
        return ResponseEntity.ok(Result.OK());
    }

    /**
     * 修改菜品状态
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/status")
    public ResponseEntity statusUpdate(@RequestParam @NotNull(message = "主键不能为空") Integer id,
                                       @RequestParam @NotNull(message = "状态不能为空") Short status){

        this.dishesService.dishStatusUpdate(status,id);

        //将所有的菜品缓存数据清理掉，所有以dish_开头的key
        cleanCache("dish_*");

        return ResponseEntity.ok(Result.OK());
    }

    /**
     * 清理缓存数据
     * @param pattern
     */
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}

